package indi.zhifa.engine.core.excel.handler;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import indi.zhifa.engine.core.common.enums.entity.EnumItemVo;
import indi.zhifa.engine.core.common.enums.entity.EnumNodeVo;
import indi.zhifa.engine.core.common.enums.service.IEnumMemoService;
import indi.zhifa.engine.core.excel.entity.BaseExcelSheetRowData;
import indi.zhifa.engine.core.excel.entity.enums.EExcelErrorCode;
import indi.zhifa.engine.core.excel.entity.exception.ExcelException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


@RequiredArgsConstructor
@Slf4j
public class EnumCellHandler implements IExcelCellHandler{

    private final IEnumMemoService mEnumMemoService;

    protected Enum getEnum(Class pClass,String pName){

        EnumNodeVo enumNodeDto = mEnumMemoService.findByClass(pClass);

        if(null == enumNodeDto){
            throw new ExcelException(EExcelErrorCode.CELL_TYPE_FAILED,"没有找到名为 "+pClass.getName()+" 的枚举类");
        }

        EnumItemVo enumItemDto = enumNodeDto.getByCName(pName);
        if(null == enumItemDto){
            throw new ExcelException(EExcelErrorCode.CELL_TYPE_FAILED,"枚举"+pClass.getName()+"没有找到名为 "+pName+" 的枚举项");
        }
        return enumItemDto.getEnumConstant();
    }

    public <T> Enum handle(String pStr, Class pClass){
        Enum theEnum = getEnum(pClass,pStr);
        return theEnum;
    }

    @Override
    public Object handle(Cell pCell, Class pClass) {
        if(null == pCell){
            return null;
        }
        String strVal = null;
        if(pCell.getCellType() == CellType.STRING){
            strVal = pCell.getStringCellValue();
        } else if(pCell.getCellType() == CellType._NONE || pCell.getCellType() == CellType.BLANK){
            strVal = null;
        }
        if(!StrUtil.isBlankIfStr(strVal)){
            strVal = null;
        }
        Enum theEnum = handle(strVal,pClass);
        return theEnum;
    }

    @Override
    public void fill(Cell pCell, BaseExcelSheetRowData pRowData, Field pField) throws IllegalAccessException, InvocationTargetException {
        Object data = pField.get(pRowData);
        if(null != data){
            Method getDescMethod = ClassUtil.getPublicMethod(pField.getType(),"getDesc");
            String sData = null;
            if(null != getDescMethod){
                Object obj = getDescMethod.invoke(data);
                if(null != obj){
                    sData = (String)obj;
                }
            }else{
                sData = data.toString();
            }
            if(null != sData){
                pCell.setCellValue(sData);
            }else{
                log.error("对象的字段 "+pField.getName()+" 不是枚举, \n"+ JSON.toJSONString(pRowData, JSONWriter.Feature.PrettyFormat));
            }
        }
    }
}
